package com.zbw.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbw.domain.Book;
import com.zbw.domain.BorrowingBooks;
import com.zbw.domain.User;
import com.zbw.domain.Vo.BorrowingBooksVo;
import com.zbw.mapper.BookMapper;
import com.zbw.mapper.BorrowingBooksMapper;
import com.zbw.mapper.UserMapper;
import com.zbw.service.IBorrowingBooksRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BorrowingBooksRecordServiceImpl implements IBorrowingBooksRecordService {
    @Resource
    private BorrowingBooksMapper borrowingBooksMapper;

    @Resource
    private BookMapper bookMapper;

    @Resource
    private UserMapper userMapper;


    @Override
    public LinkedList<BorrowingBooksVo> selectAllByPage(int pageNum) {
        Page<BorrowingBooks> objectPage= new Page<>(pageNum,5);
        borrowingBooksMapper.selectPage(objectPage,null);
        List<BorrowingBooks> borrowingBooks=objectPage.getRecords();
        if(borrowingBooks==null){
            return null;
        }
        LinkedList<BorrowingBooksVo> borrowingBooksVos = new LinkedList<>();
        for(BorrowingBooks b:borrowingBooks){
            User user = userMapper.selectById(b.getUserId());
            Book book = bookMapper.selectById(b.getBookId());
            BorrowingBooksVo borrowingBooksVo = new BorrowingBooksVo();
            borrowingBooksVo.setUser(user);
            borrowingBooksVo.setBook(book);
            //借书日期
            Date date = b.getDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateOfBorrowing = sdf.format(date);
            //还书日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, 2);//增加两个月
            Date date2 = calendar.getTime();
            String dateOfReturn = sdf.format(date2);

            borrowingBooksVo.setDateOfBorrowing(dateOfBorrowing);
            borrowingBooksVo.setDateOfReturn(dateOfReturn);
            borrowingBooksVos.add(borrowingBooksVo);
        }
        return borrowingBooksVos;
    }

    //查询当前用户的借书列表
    @Override
    public ArrayList<BorrowingBooksVo> selectAllBorrowRecord(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (null == user) {
            return null;
        }
        HashMap<String, Object> userMap = new HashMap<>();
        userMap.put("user_id",user.getUserId());
        List<BorrowingBooks> borrowingBookList = borrowingBooksMapper.selectByMap(userMap);
        if (borrowingBookList.size()==0) {
            return null;
        }

        ArrayList<BorrowingBooksVo> borrowingBooksVoList=new ArrayList<>();
        for(BorrowingBooks item:borrowingBookList){

            BorrowingBooksVo booksVo = new BorrowingBooksVo();
            Book book= bookMapper.selectById(item.getBookId());
            booksVo.setBook(book);

            //日期转换
            Date date1 = item.getDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateOfBorrowing = sdf.format(date1);
            //算出还书日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date1);
            calendar.add(Calendar.MONTH, 2);//增加两个月
            Date date2 = calendar.getTime();
            String dateOfReturn = sdf.format(date2);

            booksVo.setDateOfBorrowing(dateOfBorrowing);
            booksVo.setDateOfReturn(dateOfReturn);
            borrowingBooksVoList.add(booksVo);
        }
        return borrowingBooksVoList;
    }
}
